package cc.idoone.joddhttp.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author hemj
 * @version 2017/8/1
 */
@javax.servlet.annotation.WebServlet(name = "TestServlet", urlPatterns = "/test")
public class TestServlet extends javax.servlet.http.HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StringBuilder sb = new StringBuilder();
        sb.append(req.getMethod()).append('\n');
        sb.append(WebUtils.getHeaderMap(req)).append('\n');
        sb.append(WebUtils.getParamMap(req));

        ServletInputStream in =  req.getInputStream();
        ServletOutputStream out = resp.getOutputStream();

        out.println(sb.toString());
        out.println("--------------- body ---------------");

        int read = 0;
        byte[] buf = new byte[1024];
        while ((read = in.read(buf)) != -1) {
            out.write(buf, 0, read);
        }
    }
}
